// mircoApp/car-manage/carManage/carManage.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    radio: "0",
    licensePlate: "0",
    keyboardType: 1,
    selectInd: null,

    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init();
    this.getData();
    // wx.showModal({
    //   title: '',
    //   content: '车牌已经被绑定',
    //   confirmText: '去申诉',
    //   confirmColor: '#F15353',
    //   complete: (res) => {
    //     if (res.confirm) {
    //       wx.navigateTo({
    //         url: '/mircoApp/car-manage/carAppeal/carAppeal?name=carManage',
    //       })
    //     }
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  deleteBtn(e) {
    let {
      item
    } = e.currentTarget.dataset;
    wx.showModal({
      title: '',
      content: '确定要解绑此车辆吗？',
      complete: (res) => {
        if (res.confirm) {
          this.deleteCar(item)
        }
      }
    })
  },
  async deleteCar(item) {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.car-manage.frontend.controllers.car.untie-car"),
      data: {
        car_id: item.id
      }
    })
    if (data.result) {
      this.init();
      this.getData();
    } else {
      app.tips(data.msg);
    }
  },
  toCarAppeal() {
    wx.navigateTo({
      url: '/mircoApp/car-manage/carAppeal_list/carAppeal_list?&source=1',
    })
  },
  init() {
    this.setData({
      radio: "0",
      licensePlate: "0",
      keyboardType: 1,
      selectInd: null,
      show: false,
      page: 1,
      total_page: 0,
      isLoadMore: true,
      carList: []
    });
  },
  showTap() {
    this.setData({
      show: true,
    });
  },
  onClickLeft() {
    this.setData({
      show: false,
    });
    this.init();
  },
  async onClickRight(e) {
    //填写的车牌
    // let selectProvince = e.detail;
    let {
      selectProvince,
      license_colour,
      car_type
    } = e.detail;
    let car_info = {
      license_no: [...selectProvince].join(""),
      license_colour,
      car_type
    }
    if (app._isTextEmpty(car_info.license_no)) {
      app.tips("请输入车牌号");
      return;
    }
    if (license_colour == "0" && car_type == "0") {
      // 如果是绿牌并且是汽车的话，车牌要8为字符串
      if (car_info.license_no.length < 8) {
        app.tips("请输入正确车牌号");
        return;
      }
    } else {
      if (car_info.license_no.length < 7) {
        app.tips("请输入正确车牌号");
        return;
      }
    }
    console.log(car_info, 'ddddddddddd')
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.car-manage.frontend.controllers.car.bind-car"),
      data: {
        car_info
      }
    })
    if (data.result) {
      app.tips(data.msg);
      this.init();
      this.getData();
    } else {
      app.tips(data.msg);
    }
  },
  radioChange(e) {
    console.log(e);
    this.setData({
      radio: e.detail,
    });
  },
  radioChange_l(e) {
    this.setData({
      licensePlate: e.detail,
    });
    console.log(this.data.licensePlate);
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.car-manage.frontend.controllers.car.index")
    })
    if (data.result) {
      this.setData({
        carList: data.data.data,
        total_page: data.data.last_page,
        isLoadMore: true
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      return
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.car-manage.frontend.controllers.car.index"),
        data: {
          page: this.data.page
        }
      })
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          carList: this.data.carList.concat(data.data.data)
        })
      } else {
        this.data.isLoadMore = false;
        this.setData({
          isLoadMore: false,
          page: this.data.page - 1
        })
      }
    }
  }
});